package top.happylaishop.shop.controller.fontend;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.happylaishop.shop.dto.OrderDto;
import top.happylaishop.shop.service.CommonService;
import top.happylaishop.shop.service.OrderService;
import top.happylaishop.shop.utils.PageDto;
import top.happylaishop.shop.utils.resp.BaseResponse;
import top.happylaishop.shop.utils.resp.RestResponse;
import top.happylaishop.shop.vo.CreateOrderVo;

@RestController
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/create")
    public RestResponse<OrderDto> create(@RequestBody CreateOrderVo createOrderVo){
        return orderService.create(commonService.getCurrentUserInfo(), createOrderVo);
    }

    @GetMapping("/cancel/{orderNo}")
    public BaseResponse cancel(@PathVariable String orderNo){
        return orderService.cancel(commonService.getCurrentUserInfo(), orderNo);
    }

    @GetMapping("/detail/{orderNo}")
    public RestResponse<OrderDto> getDetailByOrderNo(@PathVariable String orderNo){
        return orderService.getDetailByOrderNo(commonService.getCurrentUserInfo(), orderNo);
    }

    @GetMapping("/list/{pageNum}/{pageSize}")
    public RestResponse<PageDto<OrderDto>> list(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageDto<OrderDto> orderPageDto = new PageDto<>();
        orderPageDto.setPage(pageNum == null ? 1 : pageNum);
        orderPageDto.setSize(pageSize == null ? 1 : pageSize);
        orderService.list(commonService.getCurrentUserInfo(), orderPageDto);
        return RestResponse.defaultSucessData(orderPageDto);
    }

    @GetMapping("/getCurrentOrder")
    public RestResponse<OrderDto> getCurrentOrder(){
        return orderService.getCurrentOrder(commonService.getCurrentUserInfo());
    }
}

